package insta.project.player;

import insta.project.R;
import insta.project.SongsActivity;
import android.app.Activity;
import android.content.ContentResolver;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

/**
 * Play Music
 * 
 * @author candy
 * 
 */
public class PlayMusicActivity extends Activity implements
		SeekBar.OnSeekBarChangeListener, OnBufferingUpdateListener,
		OnCompletionListener {

	private MediaPlayer mMediaPlayer;
	private int position;
	private Audio currentAudio;

	SeekBar mSeekBar;
	TextView mProgressText;
	TextView mTrackingText;
	ImageView mPlayPause;
	private Handler mHandler = new Handler();

	ContentResolver crContentResolver;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playmusic);

		position = this.getIntent().getExtras().getInt("position");
		currentAudio = new Audio();
		currentAudio.setFilename(this.getIntent().getStringExtra("audio_data"));
		currentAudio.setTitle(this.getIntent().getStringExtra("audio_title"));
		currentAudio.setArtist(this.getIntent().getStringExtra("audio_artist"));
		currentAudio
				.setImageuri(this.getIntent().getStringExtra("audio_image"));

		display(currentAudio);

		// *********************************** //
		// ******* DISPLAY RATINGBAR ********* //
		// *********************************** //

		// final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
		// Log.d("rating bar", "" + ratingbar);

		// ratingbar.setFocusable(false);
		// necessaire pour activer le onclick item
		// ratingbar
		// .setOnRatingBarChangeListener(new
		// RatingBar.OnRatingBarChangeListener() {
		// public void onRatingChanged(RatingBar ratingBar,
		// float rating, boolean fromUser) {
		// ratingbar.setRating(rating);
		// Toast.makeText(PlayMusicActivity.this,
		// "rating:" + String.valueOf(rating),
		// Toast.LENGTH_LONG).show();
		// }
	}

	public void display(Audio _audio) {

		ImageView jaquette = (ImageView) findViewById(R.id.avatar);
		if (_audio.getImageuri() != null) {
			jaquette.setImageURI(Uri.parse(_audio.getImageuri()));
		} else {
			jaquette.setImageResource(R.drawable.default_cover);
		}

		TextView title = (TextView) findViewById(R.id.title);
		title.setText(_audio.getTitle());

		TextView artist = (TextView) findViewById(R.id.artist);
		artist.setText(_audio.getArtist());

		mPlayPause = (ImageView) findViewById(R.id.playpause);

		mProgressText = (TextView) findViewById(R.id.progress);
		mTrackingText = (TextView) findViewById(R.id.tracking);

		mMediaPlayer = new MediaPlayer();
		mMediaPlayer.setOnBufferingUpdateListener(this);
		mMediaPlayer.setOnCompletionListener(this);

		try {
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.reset();
			}
			mMediaPlayer.setDataSource(_audio.getFilename());
			mMediaPlayer.prepare();

			mSeekBar = (SeekBar) findViewById(R.id.seekbar);
			mSeekBar.setMax(mMediaPlayer.getDuration());
			mSeekBar.setOnSeekBarChangeListener(this);

			mMediaPlayer.start();
			primarySeekBarProgressUpdater();
			mPlayPause.setImageResource(R.drawable.pause);
			mPlayPause.setTag("play");

		} catch (Exception e) {

		}
	}

	/**
	 * Method which updates the SeekBar primary progress by current song playing
	 * position
	 */
	private void primarySeekBarProgressUpdater() {
		mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
		if (mMediaPlayer.isPlaying()) {
			Runnable notification = new Runnable() {
				public void run() {
					primarySeekBarProgressUpdater();
				}
			};
			mHandler.postDelayed(notification, 1000);
		}
	}

	public void onBackPressed() {
		super.onBackPressed();
		mMediaPlayer.stop();
		finish();
	}

	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromTouch) {
		if (fromTouch) {
			mMediaPlayer.seekTo(progress);
			seekBar.setProgress(progress);
		}
	}

	public void onStartTrackingTouch(SeekBar seekBar) {
		seekBar.setSecondaryProgress(seekBar.getProgress());
		//mTrackingText.setText("trackingOn");
	}

	public void onStopTrackingTouch(SeekBar seekBar) {
		//mTrackingText.setText("trackingoff");
	}

	public void onClickPlayPause(View v) {
		switch (v.getId()) {
		case R.id.playpause:
			if (mPlayPause.getTag().equals("pause")) {
				mPlayPause.setTag("play");
				mMediaPlayer.start();
				primarySeekBarProgressUpdater();
				mPlayPause.setImageResource(R.drawable.pause);
			} else if (mPlayPause.getTag().equals("play")) {
				if (mMediaPlayer.isPlaying()) {
					mPlayPause.setTag("pause");
					mMediaPlayer.pause();
					mPlayPause.setImageResource(R.drawable.play);
				}
			}
			break;
		}
	}

	public void onClick(View v) {

		boolean continue_move = false;

		switch (v.getId()) {
		case R.id.previous:
			Log.d("button_click", "previous");
			if (position > 0) {
				position--;
				continue_move = true;
			}
			break;
		case R.id.next:
			Log.d("button_click", "next");
			if (position + 1 < SongsActivity.musics.size()) {
				position++;
				continue_move = true;
			}
			break;
		}

		if (continue_move) {
			mMediaPlayer.stop();
			Log.d("position", "" + position);
			Log.d("musics_tab", "" + SongsActivity.musics.size());

			currentAudio = SongsActivity.musics.get(position);
			display(currentAudio);
		}
	}

	public void onCompletion(MediaPlayer mp) {
		// Trying to get next song
		int nNextIndex = (this.position + 1 >= SongsActivity.musics.size()) ? 0
				: this.position + 1;
		Log.d("player.play", String.valueOf(nNextIndex));
		this.display(SongsActivity.musics.get(nNextIndex));

	}

	public void onBufferingUpdate(MediaPlayer mp, int percent) {
	}

}